﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vngoodlife.Models;
using System.Web.Routing;
using vngoodlife.Singleton;
using vngoodlife.Util;
using System.IO;

namespace vngoodlife.Controllers
{
    public class MemberController : Controller
    {
        #region logon

        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        public ActionResult LogOn()
        {
            if (Request.IsAuthenticated)
            {
                return RedirectToAction("DashBoard", "Member");
            }
            else
            {
                return View("/Views/Backend/Admin/LogOn.cshtml");
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult LogOn(LogOnMemberModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, model.RememberMe);
                    DataSingleton.Instance.GetMemberModel().UpdateLogInTime(model.UserName);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("DashBoard", "Member");
                    }
                }
                else
                {
                    //  ModelState.AddModelError("", "The user name or password provided is incorrect."); throw a mesg to view at the Html.ValidationSummary
                    ModelState.AddModelError("", "");
                }
            }
            // If we got this far, something failed, redisplay form
            return View("/Views/Backend/Admin/LogOn.cshtml");
        }

        public ActionResult LogOff()
        {
            DataSingleton.Instance.GetMemberModel().UpdateLogOutTime(HttpContext.User.Identity.Name);
            FormsService.SignOut();
            return RedirectToAction("Index", "Home");
        }

        #endregion


        

        #region Member
        public ActionResult MemberPopup(string memberName)
        {
            return PartialView("/Views/FrontEnd/Article/_MemberPopup.cshtml"
                  , DataSingleton.Instance.GetLinQContext().Members.SingleOrDefault(u=>u.MemberName== memberName));
        }

        [ChildActionOnly]
        public ActionResult BuildLogOnControl()
        {
            return PartialView("/Views/Shared/FrontEnd/_LogOnControl.cshtml");
        }

        #endregion

    }
}
